/*
 * @Date: 2022-11-05 16:55:34
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-11-24 10:29:55
 * @FilePath: \vue-admin-ts\src\utils\request\service.ts
 * @Description: axios封装
 */
import axios from "axios";
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import { ElLoading } from "element-plus";
//拦截器接口定义
interface SailorReaquestInterceptors<T = AxiosResponse> {
  requestInterceptor?: (config: AxiosRequestConfig) => AxiosRequestConfig;
  requestInterceptorCatch?: (err: any) => any;
  responseInterceptor?: (res: T) => T;
  responseInterceptorCatch?: (err: any) => any;
}
interface SailorRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
  interceptor?: SailorReaquestInterceptors<T>;
  showLoading?: boolean;
}
class SAILORRequest {
  instance: AxiosInstance;
  interceptor?: SailorReaquestInterceptors;
  //加载实例
  loadingInstance?: any;
  showLoading: boolean;
  constructor(config: SailorRequestConfig) {
    this.instance = axios.create(config);
    this.interceptor = config.interceptor;
    this.showLoading = config.showLoading ?? true;
    //请求拦截器
    this.instance.interceptors.request.use(
      this.interceptor?.requestInterceptor,
      this.interceptor?.requestInterceptorCatch,
    );
    //响应拦截器
    this.instance.interceptors.response.use(
      this.interceptor?.responseInterceptor,
      this.interceptor?.responseInterceptorCatch,
    );
    //全部请求拦截
    this.instance.interceptors.request.use(
      (config) => {
        if (this.showLoading) {
          this.loadingInstance = ElLoading.service({
            lock: true,
            text: "正在加载请稍后...",
            background: "rgba(0,0,0,0.7)",
          });
        }
        return config;
      },
      (err) => {
        return err;
      },
    );
    //全部响应拦截
    this.instance.interceptors.response.use(
      (res) => {
        this.loadingInstance?.close();
        return res.data;
      },
      (err) => {
        this.loadingInstance?.close();
        return err;
      },
    );
  }
  request<T = any>(config: SailorRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptor?.requestInterceptor) {
        config = config.interceptor.requestInterceptor(config);
      }
      if (config.showLoading === false) {
        this.showLoading = config.showLoading;
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptor?.responseInterceptor) {
            res = config.interceptor?.responseInterceptor(res);
          }
          resolve(res);
          this.showLoading = true;
        })
        .catch((err) => {
          this.showLoading = true;
          reject(err);
          return err;
        });
    });
  }
  get<T = any>(config: SailorRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: "GET" });
  }
  post<T = any>(config: SailorRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: "POST" });
  }
  delete<T = any>(config: SailorRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: "DELETE" });
  }
  patch<T = any>(config: SailorRequestConfig<T>): Promise<T> {
    return this.request({ ...config, method: "PATCH" });
  }
}

export default SAILORRequest;
